package com.sc.controller.vehicle;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.domain.VehicleRegion;
import com.sc.dto.VehicleRegionNamePageDTO;
import com.sc.result.PageResult;
import com.sc.result.Result;
import com.sc.service.vehicle.IVehicleRegionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 车辆管理模块
 */
@RestController
@RequestMapping("/vehicleRegion")
@Api(tags = "车辆区域相关接口")
@CrossOrigin
public class VehicleRegionController {
    @Autowired
    IVehicleRegionService ivrs;
    /**
     * 添加区域
     * @param vehicleRegion
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("添加区域")
    public Result add(@RequestBody VehicleRegion vehicleRegion){
        ivrs.addVehicleRegion(vehicleRegion);
        return Result.success();
    }
    /**
     * 查询区域列表
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("查询区域列表")
    public PageResult list(Integer pageNum, Integer pageSize){
        Page<VehicleRegion> page = ivrs.showRegions(pageNum, pageSize);
        return new PageResult(page.getTotal(),page.getRecords());
    }
    /**
     * 查询区域编号+名称
     * @return
     */
    @GetMapping("/listName")
    @ApiOperation("查询区域编号和名称")
    public Result<List<String>> listName(){
        List<VehicleRegion> list = ivrs.list();
        List<String> nameAndNos = list.stream().map(p -> p.getName() + p.getNo())
                .collect(Collectors.toList());
        return Result.success(nameAndNos);
    }
    /**
     * 查询区域详情
     * @return
     */
    @GetMapping("/selectDetails")
    @ApiOperation("查询区域详情")
    public Result<VehicleRegion> selectDetails(Integer id){
        VehicleRegion region = ivrs.getRegionById(id);
        return Result.success(region);
    }

    /**
     * 修改区域
     * @param vehicleRegion
     * @return
     */
    @PutMapping("/update")
    @ApiOperation("修改区域")
    public Result update(@RequestBody VehicleRegion vehicleRegion){
        ivrs.addVehicleRegion(vehicleRegion);
        return Result.success();
    }

    /**
     * 删除区域
     * @return
     */
    @DeleteMapping("/delete")
    @ApiOperation("删除区域")
    public Result delete(Integer id){
        ivrs.deleteVehicleRegion(id);
        return Result.success();
    }

    @DeleteMapping("/deleteByIds")
    @ApiOperation("批量删除")
    public Result deleteByIds(@RequestBody List<Integer> ids) {
        ivrs.removeByIds(ids);
        return Result.success();
    }

    /**
     * 根据名称查询
     */
    @GetMapping("/selectByName")
    @ApiOperation("根据区域名分页查询")
    public PageResult selectByName(VehicleRegionNamePageDTO regionNamePageDTO){
        Page<VehicleRegion> page = ivrs.PageQueryByRegionName(regionNamePageDTO);
        return new PageResult(page.getTotal(),page.getRecords());
    }
}
